package com.hk.Mgr.web.controller;

import com.hk.api.ProcessAPI;
import com.hk.base.BaseCtrl;
import com.hk.commons.ResultCode;
import com.hk.commons.entity.sys.SysUser;
import com.hk.commons.entity.view.UserView;
import com.hk.commons.vo.JsonResult;
import com.hk.service.sys.ISysUser;
import com.hk.service.view.IUserView;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 *专门为其它项目提供服务
 * 1-查询用户信息
 * @author Evan
 * @date 2017/11/9
 */
@Controller
@RequestMapping(value = BaseCtrl.CommonRootPath + "/supply")
public class SupplyController extends BaseCtrl{
    @Autowired
    private IUserView userViewService;

    @Autowired
    private ISysUser userService;

    @ResponseBody
    @RequestMapping(value = "/getUserView", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public JsonResult getUserInfo(HttpServletRequest request,Long userId,String phone){
        ProcessAPI api = (result, validate) -> {
            Long id = userId;
            if(StringUtils.isNotBlank(phone) && userId == null){
                SysUser user = userService.getByPhoneNum(phone);
                if(user == null){
                    result.resultCode = ResultCode.FAILED.getCode();
                    result.msg = "用户phone:"+phone+"不存在！";
                    return;
                }
                id = user.getId();
            }
            UserView userView = userViewService.findById(id);
            if(userView == null){
                result.resultCode = ResultCode.FAILED.getCode();
                result.msg = "用户id:"+id+"不存在！";
                return;
            }
            result.result = userView;
        };
        return api.process(request);

    }

}
